<template>
  <page-form-row>
    <page-form-item field="refCostType" label="关联单据类型" :size="form.ifOpenCheck ? '1/2' : '1/3'">
      <a-select
        :disabled="routeQuery.costParams || routeQuery.orderParams"
        placeholder="请选择"
        v-model="form.refCostType"
        @change="changeRefCostType"
      >
        <a-option v-for="item in dict.refCostTypeEnum" :key="item.value" :value="item.value" :label="item.label" />
      </a-select>
    </page-form-item>
  </page-form-row>
  <page-form-row>
    <!-- 选择关联工单 -->
    <template v-if="form.refCostType == 1">
      <page-form-item field="workType" label="关联工单类型" :size="form.ifOpenCheck ? '1/2' : '1/3'">
        <a-select
          :allow-clear="true"
          :disabled="routeQuery.costParams"
          placeholder="请选择"
          v-model="form.workType"
          @change="changeWorkInfo"
        >
          <a-option v-for="item in dict.workTypeEnum" :key="item.value" :value="item.value" :label="item.label" />
        </a-select>
      </page-form-item>
      <page-form-item field="workId" label="关联工单号" :size="form.ifOpenCheck ? '1/2' : '1/3'">
        <i-input v-if="routeQuery.costParams" disabled v-model="form.workCode" />
        <template v-else>
          <i-input v-if="!form.workType" disabled placeholder="请先选择关联工单类型" />
          <remote-select
            v-else
            :disabled="routeQuery.costParams"
            v-model="form.workId"
            type="getWorkCode"
            :params="{
              workTypeList: [form.workType],
              workOperatorList: [form.loginUserId],
            }"
            @change="changeWorkFn"
          />
        </template>
      </page-form-item>
      <page-form-item label="关联业务单信息" :size="form.ifOpenCheck ? '1/2' : '1/3'">
        <i-input disabled v-model="form.workInfo" />
      </page-form-item>
    </template>
    <!-- 选择关联业务单 -->
    <template v-if="form.refCostType == 2">
      <page-form-item field="businessType" label="关联业务类型" :size="form.ifOpenCheck ? '1/2' : '1/3'">
        <a-select
          :allow-clear="true"
          :disabled="routeQuery.orderParams"
          placeholder="请选择"
          v-model="form.businessType"
          @change="changeBusinessInfo"
        >
          <a-option v-for="item in dict.businessTypeEnum" :key="item.value" :value="item.value" :label="item.label" />
        </a-select>
      </page-form-item>
      <page-form-item field="businessId" label="关联业务单号" :size="form.ifOpenCheck ? '1/2' : '1/3'">
        <i-input v-if="routeQuery.orderParams" disabled v-model="form.businessCode" />
        <template v-else>
          <i-input v-if="!form.businessType" disabled placeholder="请先选择关联业务单类型" />
          <remote-select
            :disabled="routeQuery.orderParams"
            v-if="form.businessType == 1"
            v-model="form.businessId"
            type="getShortCode"
            @change="changeBusinessFn"
          />
          <remote-select
            :disabled="routeQuery.orderParams"
            v-if="form.businessType == 2"
            v-model="form.businessId"
            type="getLongCode"
            @change="changeBusinessFn"
          />
        </template>
      </page-form-item>
    </template>
    <!-- 承租人信息 -->
    <page-form-item label="承租人信息" :size="form.ifOpenCheck ? '1/2' : '1/3'" v-if="form.refCostType == 1 || form.refCostType == 2">
      <i-input disabled v-model="form.memberInfo" />
    </page-form-item>
    <!-- 费用产生门店 -->
    <page-form-item field="feeOrgId" label="费用产生门店" :size="form.ifOpenCheck ? '1/2' : '1/3'">
      <i-input v-if="form.refCostType !== 3" disabled v-model="form.feeOrgName" />
      <remote-select v-else v-model="form.feeOrgId" type="org_add_listByStaff" @change="changeOrg" />
    </page-form-item>
  </page-form-row>
</template>

<script setup>
  import { inject } from 'vue'
  import { usePageDict } from '@/hooks/useDict'
  import { useRoute } from 'vue-router'

  const route = useRoute()
  const routeQuery = route.query
  const dict = usePageDict('getCostEnums')

  const form = inject('form')

  const emit = defineEmits(['getRefCarInfoList'])

  // 关联业务单信息

  // 更改关联单据类型
  const changeRefCostType = () => {
    form.value.workType = ''
    changeWorkInfo()
    changeBusinessInfo()
  }

  // 改变关联工单类型，清空级联信息
  const changeWorkInfo = () => {
    form.value.workCode = ''
    form.value.workId = ''
    form.value.workInfo = '-'
    form.value.memberInfo = '-'
    form.value.feeOrgId = ''
    form.value.feeSiteId = ''
    form.value.feeOrgName = '-'

    form.value.businessId = ''
    form.value.businessCode = ''
    form.value.businessType = ''
    form.value.feeList.forEach((item) => {
      item.carId = ''
      item.licenseNo = ''
      item.carModel = '-'
    })
  }
  // 选中关联工单号，回显关联业务单信息
  const changeWorkFn = (val, option) => {
    if (val) {
      form.value.feeOrgId = option.orderInfo?.siteId
      form.value.feeSiteId = option.orderInfo?.siteId
      form.value.feeOrgName = option.orderInfo?.siteName
      form.value.workCode = option.workCode

      form.value.workInfo = option.orderInfo?.orderTypeName + option.refCode || '-'
      form.value.memberInfo = option.orderInfo?.memberName + option.orderInfo?.memberPhone || '-'
      form.value.businessId = option.refId
      form.value.businessCode = option.refCode
      form.value.businessType = option.refType

      form.value.carId = option.carId
      form.value.feeList.forEach((item) => {
        item.carId = ''
        item.licenseNo = ''
        item.carModel = '-'
      })
      emit('getRefCarInfoList')
    } else {
      form.value.feeOrgId = null
      form.value.feeSiteId = null
      form.value.feeOrgName = '-'
      form.value.workCode = null
      form.value.workInfo = '-'
      form.value.memberInfo = '-'
      form.value.businessId = null
      form.value.businessCode = null
      form.value.businessType = null
      form.value.carId = null
    }
  }
  // 改变关联业务单类型，清空级联信息
  const changeBusinessInfo = () => {
    form.value.feeOrgId = ''
    form.value.feeSiteId = ''
    form.value.feeOrgName = '-'

    form.value.businessId = ''
    form.value.businessCode = ''

    form.value.memberInfo = '-'
    form.value.feeList.forEach((item) => {
      item.carId = ''
      item.licenseNo = ''
      item.carModel = '-'
    })
  }
  // 选中关联业务单号，回显关联业务单信息
  const changeBusinessFn = (val, option) => {
    form.value.businessCode = form.value.businessType == 1 ? option.orderCode : option.orderNo
    form.value.feeOrgId = option.siteId
    form.value.feeSiteId = option.siteId
    form.value.feeOrgName = option.siteName
    form.value.memberInfo = option.memberName + option.memberPhone || '-'
    form.value.feeList.forEach((item) => {
      item.carId = ''
      item.licenseNo = ''
      item.carModel = '-'
    })
    emit('getRefCarInfoList')
  }
  // 更改费用产生门店
  const changeOrg = (val, option) => {
    form.value.feeSiteId = option.orgId
  }
</script>

<style lang="less" scoped></style>
